/**
 * <p>Title: RegularSMS.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-26 上午07:39:53  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.common.zm.basetools.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.ct.SMSModel;
import com.baosight.sczx.common.zm.cInterface.ct.SystemSet;

/**
 * 定时消息生成
 * <p>Title: RegularSMS.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-26 上午07:39:53  张志芳  Create
 *</pre>
 */
public class RegularSMS {
	private int timeInterval = Integer.parseInt(SystemSet.getInstance().getFdSystemFrequency());
	private String beginTime = StaticTools.getDateTime();
	private String endTime = StaticTools.getDateTime(timeInterval, beginTime, "yyyy-MM-dd HH:mm:ss");
	private SMSModel _model;
	private Dao dao;

	
	public RegularSMS(Dao dao){
		this.dao = dao;
		_model = new SMSModel(dao);
	}
	
	/**
	 * 生成定时短信
	 * <p>Function: MakeSMS</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-28上午08:51:42 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void MakeSMS()throws Exception{
		Map _map = new HashMap();
		_map.put("fdSmsType", "定时消息");
		_map.put("fdStatus", "在用");
		List<Map> list=dao.query("ZMCTSmsModel.querySMSModel", _map);		
		
		for(int i=0; i<list.size(); i++)			
		{
			Map modelMap = list.get(i);
			for(int j=1; j<=4; j++)//循环四个时间段
			{
				Object time = list.get(i).get("fdTimingTime"+j);				
				if(time == null || "".equals(time)) continue;//判断是否设置了这个生产时间
				
				time = calculateDateTime(list.get(i).get("fdFrequency"), time, beginTime.substring(0, 10));	//计算具体的日期时间			
				Object sendTime = list.get(i).get("fdSendingTime"+j);
				sendTime = calculateDateTime(list.get(i).get("fdFrequency"), sendTime, beginTime.substring(0, 10));
				
				//判断模板设置的生成时间是否在本次定时活动的范围内
				if(beginTime.compareTo(time.toString()) <= 0 && endTime.compareTo(time.toString()) > 0)
				{					
					_model.insertRegularSMS(modelMap, sendTime.toString());
				}
			}
		}
	}
	
	/**
	 * 发送定时短信
	 * <p>Function: SendSMS</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-28上午08:51:29 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void SendSMS()
	{
		List<Map> list=dao.query("ZMCTSms.querySMS",null);	
		
		for(int i=0; i<list.size(); i++)			
		{
			Object time = list.get(i).get("fdPlanSendTime");				
			if(time != null && !"".equals(time))
			{			
				//判断模板设置的生成时间是否在本次定时活动的范围内
				if(beginTime.compareTo(time.toString()) <= 0 && endTime.compareTo(time.toString()) > 0)
				{
					String content = list.get(i).get("fdSmsContent").toString();
					String mobilePhone = list.get(i).get("fdMobilePhone").toString();
					String createTime = list.get(i).get("fdCreateTime").toString();
					_model.generateSMS(content, mobilePhone, createTime);
					
					Object fdKeyId = list.get(i).get("fdKeyId");
					Map map = new HashMap();
					map.put("fdKeyId", fdKeyId);
					map.put("fdStatus", "已发送");
					map.put("fdSendTime", StaticTools.getDateTime());
					dao.update("ZMCTSms.updateStatus", map);
				}
			}
		}
	}
	
	/**
	 * 根据设置的频率和设置点计算具体的日期时间
	 * <p>Function: calculateDateTime</p>
	 * <p>Description: frequency频率，setStr设置点，nowDate当前日期</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-7-3上午02:47:56 张志芳
	 * </p>
	 *
	 */
	private String calculateDateTime(Object frequency, Object setStr, String nowDate)
	{
		if("每日".equals(frequency))
		{
			return nowDate + " " + setStr + ":00";
		}
		else if("每周".equals(frequency))
		{
			
		}
		else if("每月".equals(frequency))
		{
			
		}
		return null;
	}
	

}
